package com.beauty.dressing.module.market;

import android.graphics.Rect;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.beauty.dressing.common.bean.Ad;
import com.beauty.dressing.common.bean.CourseBean;
import com.beauty.dressing.common.bean.GroupNavigation;
import com.beauty.dressing.common.bean.GuideList;
import com.beauty.dressing.common.utils.ResUtil;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.common.view.BannerLayout;
import com.beauty.dressing.module.main.adapter.CourseListAdapter;
import com.beauty.dressing.module.market.activity.ShopCarActivity;
import com.beauty.dressing.module.market.adapter.GoodsListAdapter;
import com.framework.common.utils.IDisplayUtil;
import com.framework.library.haorefresh.HaoRecyclerView;
import com.framework.library.haorefresh.ProgressView;
import com.xiaolanba.commonlib.fresco.FrescoUtil;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.bean.BaseBean;
import com.beauty.dressing.common.bean.MultiShopList;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.common.view.LoadingNodataLayout;
import com.beauty.dressing.module.market.adapter.MarketMainAdapter;
import com.beauty.dressing.module.market.presenter.ShopPresenter;
import com.beauty.dressing.module.market.presenter.contract.ShopContract;
import com.beauty.dressing.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 商城
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/05/02
 */

public class MarketFragment extends BaseFragment{

    protected HaoRecyclerView mListView;
    protected RecyclerView recyclerView;
    private GoodsListAdapter mAdapter;
    private BannerLayout mBannerHeader;
    private List<BaseBean> mList = new ArrayList<>();
    SwipeRefreshLayout mSwipeRefresh;
    @Override
    public void setContentView() {
        setContentView(R.layout.common_haorefresh_no_title);
    }

    @Override
    public void findView() {
        mListView = (HaoRecyclerView) findViewById(R.id.hao_recycleview);
        mListView.setBackgroundColor(ResUtil.getColor(R.color.white));
        mBannerHeader = new BannerLayout(context);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return mAdapter.getItemViewType(position) == GoodsListAdapter.ViewType.COURSE_ITEM ? 1 : 2;
            }
        });
        mListView.setLayoutManager(gridLayoutManager);
        mListView.addOnScrollListener(FrescoUtil.getPauseOnScrollListener());
        final int dip12 = IDisplayUtil.dip2px(context,12);
        final int dip3 = IDisplayUtil.dip2px(context,3);
        final int headerCount = 1;
        mListView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int position = parent.getChildLayoutPosition(view);
                if (position < mAdapter.getChildCount() + headerCount) { //有header的话，需要加上header数量
                    int type = mAdapter.getItemViewType(position);
                    outRect.top = outRect.bottom = 0;
                    if (type == CourseListAdapter.ViewType.COURSE_ITEM) {
                        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
                        int spanIndex = layoutParams.getSpanIndex();
                        Log.e("mm","---COURSE_ITEM view.spanIndex="+spanIndex+",pos= "+position);
                        if (spanIndex ==0) {
                            outRect.left = dip12;
                            outRect.right = dip3;
                        } else {
                            outRect.left = dip3;
                            outRect.right = dip12;
                        }
                    } else {
                        outRect.left = outRect.right = 0;
                    }
                }
            }
        });

        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
        mSwipeRefresh.setColorSchemeResources(R.color.theme_blue, R.color.theme_blue, R.color.theme_blue);
        //设置自定义加载中和到底了效果
        ProgressView progressView = new ProgressView(context);
        progressView.setIndicatorId(ProgressView.BallPulse);
        progressView.setIndicatorColor(ResUtil.getColor(R.color.theme_blue));
        mListView.setFootLoadingView(progressView);
        TextView textView = new TextView(context);
        textView.setText("全部加载完成");
        mListView.setFootEndView(textView);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LBController.MainHandler.postDelayed(new Runnable() {
                    public void run() {
                        mSwipeRefresh.setRefreshing(false);

                    }
                }, 2000);

            }
        });
        mListView.loadMoreEnd();
    }

    @Override
    public void initData() {
        mAdapter = new GoodsListAdapter(context);
        mAdapter.addHeaderView(mBannerHeader);
        mList.add(new GuideList());
        mList.add(new GroupNavigation(0,"推荐商品",false,true));
        for (int i=0;i<8;i++){
            mList.add(new CourseBean());
        }
        mList.add(new GroupNavigation(0,"色料盒套",false,true));
        for (int i=0;i<5;i++){
            mList.add(new CourseBean());
        }
        mList.add(new GroupNavigation(0,"仪器",false,true));
        for (int i=0;i<8;i++){
            mList.add(new CourseBean());
        }
        mList.add(new GroupNavigation(0,"色料",false,true));
        for (int i=0;i<10;i++){
            mList.add(new CourseBean());
        }
        mListView.setAdapter(mAdapter);
        mAdapter.setChild(mList);
        List<Ad> adList = new ArrayList<>();
        adList.add(new Ad("http://images.9zhitx.com/a97d8b8b-d703-46c3-9700-b966c8fa7dcb.jpg",null));
        adList.add(new Ad("http://images.9zhitx.com/ac870ecd-26f4-4670-9364-8a542b4e322a.jpg",null));
        adList.add(new Ad("https://tvax2.sinaimg.cn/crop.0.0.664.664.180/006zWmitly8fgxj1jcsevj30ig0igt9h.jpg",null));
        mBannerHeader.setBannerPagerAdapter(adList,true);
    }

    public void homeTabRefresh(){

    }
}
